package com.atguigu.gmall.mq.config;

import org.springframework.amqp.core.*;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

import java.util.HashMap;
import java.util.Map;

/**
 * 基于死信的 Java配置类
 *  spring.xml
 */
@Configuration
public class DeadLetterMqConfig {

    public static final String exchange_dead = "exchange.dead";
    public static final String routing_dead_1 = "routing.dead.1";
    public static final String routing_dead_2 = "routing.dead.2";
    public static final String queue_dead_1 = "queue.dead.1";
    public static final String queue_dead_2 = "queue.dead.2";

    // 正常或死信交换机
    @Bean
    public Exchange deadLetterExchange(){
        //直接交换机
        return ExchangeBuilder.directExchange(exchange_dead).build();

    }
    //特殊队列
    @Bean
    public Queue ttlQueue(){
        Map<String, Object> arguments = new HashMap<>();
        // 毫秒   ttl
        arguments.put("x-message-ttl",60000);
        //死信交换机
        arguments.put("x-dead-letter-exchange",exchange_dead);
        //死信RoutingKey
        arguments.put("x-dead-letter-routing-key",routing_dead_2);
        return QueueBuilder.durable(queue_dead_1).withArguments(arguments).build();
    }

    //正常的队列
    @Bean
    public Queue queue2(){
        return QueueBuilder.durable(queue_dead_2).build();
    }

    //绑定
    @Bean
    public Binding bindingTtlQueueToDeadLetterExchange(){
        return BindingBuilder.bind(ttlQueue()).to(deadLetterExchange()).with(routing_dead_1).noargs();
    }
    //绑定
    @Bean
    public Binding bindingQueue2ToDeadLetterExchange(){
        return BindingBuilder.bind(queue2()).to(deadLetterExchange()).with(routing_dead_2).noargs();
    }

}